package com.example.day13_7_13_1;


import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager mManager;
    private TextView mTv;
    private TextView mTv2;
    private TextView mTv3;
    private Vibrator mVibrator;
    private TextView mTv4;
    private TextView mTv5;
    private TextView mTv6;
    private TextView mTv7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        mManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        List<Sensor> sensorList = mManager.getSensorList(Sensor.TYPE_ALL);
        for (int i = 0; i < sensorList.size(); i++) {
            Log.d("tag", "onCreate: " + sensorList.get(i).getName());
        }


    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        int type = event.sensor.getType();
        if (type == Sensor.TYPE_ACCELEROMETER) {
            float x = event.values[0];
            float y = event.values[1];
            float z = event.values[2];
            Log.d("tag", "x: " + x);
            mTv.setText("x:" + x);
            mTv2.setText("y:" + y);
            mTv3.setText("z:" + z);

            if (Math.abs(x) > 17 || Math.abs(y) > 17 && Math.abs(z) > 17) {
                shake();
            }
        } else if (type == Sensor.TYPE_ORIENTATION) {
            float z = event.values[0];
            float x = event.values[1];
            float y = event.values[2];
            mTv4.setText("绕z轴转过:"+z);
            mTv5.setText("绕x轴转过:"+x);
            mTv6.setText("绕y轴转过:"+y);
        }else if (type == Sensor.TYPE_LIGHT){
            float x = event.values[0];
            mTv7.setText("光照强度:"+x);
        }

    }

    private void shake() {
        MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.weichat_audio);
        mediaPlayer.start();
        long[] patter = {1000, 1000, 2000, 50};
        mVibrator.vibrate(patter, -1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mManager.registerListener(this,
                mManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        mManager.registerListener(this,
                mManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_NORMAL);
        mManager.registerListener(this,
                mManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mManager.unregisterListener(this);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    private void initView() {
        mTv = (TextView) findViewById(R.id.tv);
        mTv2 = (TextView) findViewById(R.id.tv2);
        mTv3 = (TextView) findViewById(R.id.tv3);
        mTv4 = (TextView) findViewById(R.id.tv4);
        mTv5 = (TextView) findViewById(R.id.tv5);
        mTv6 = (TextView) findViewById(R.id.tv6);
        mTv7 = (TextView) findViewById(R.id.tv7);
    }
}
